package com.young.financial.utils;

import org.apache.commons.codec.binary.Hex;

import com.auth0.jwt.JWTSigner;
import com.auth0.jwt.JWTVerifier;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;


public class JWT {


    /**
     * 解密
     *
     * @param jwt
     * @param classT
     * @param <T>
     * @return //解密，传入一个加密后的token字符串和解密后的类型
     */
    //private static final String secret = "2d7204ac93024094ae07e6d953dcd86b";
    private static final String EXP = "exp";
    private static final String PAYLOAD = "payload";

    public static Map<String, Object> unsign(String jwt, String secret) {
        JWTVerifier verifier = new JWTVerifier(secret);
        try {
            Map<String, Object> claims = verifier.verify(jwt);
            return claims;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * https://blog.csdn.net/change_on/article/details/71191894
     * //加密，传入一个对象和有效期
     * wgq
     */
    public static String sign(String secret, long maxAge) {
        try {
            JWTSigner signer = new JWTSigner(secret);
            Map<String, Object> claims = new HashMap<String, Object>();
            claims.put(EXP, System.currentTimeMillis() + maxAge);
            return signer.sign(claims);
        } catch (Exception e) {
            return null;
        }
    }




   /* public static void main(String[] args) {
        String jwt = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1ODY4NzI3NTI2MDUsInBheWxvYWQiOiJ7XCJpZFwiOjEsXCJ1c2VybmFtZVwiOlwibXRneVwiLFwicGFzc3dvcmRcIjpcIjEyMzQ1NlwiLFwiZmxhZ1wiOlwicGxhdGZvcm1fMDFcIixcInRva2VuXCI6bnVsbCxcInN0YXRlXCI6XCIxXCJ9In0.lV9AF2E5h-wpPjH-bGc4CRiigTjARVq0INqoThtWNJQ";
        PlatformLogin login = JWT.unsign(jwt, PlatformLogin.class);
        PlatformLogin platformLogin = new PlatformLogin();
        platformLogin.setFlag("platform_01");
        platformLogin.setId(1);
        platformLogin.setPassword("123456");
        platformLogin.setState("1");
        platformLogin.setUsername("mtgy");
        System.out.println(JWT.sign(platformLogin,31556926000L));


    }*/

    /**
     * 生成含有随机盐的密码
     */
    public static String generate(String password) {
        Random r = new Random();
        StringBuilder sb = new StringBuilder(16);
        sb.append(r.nextInt(99999999)).append(r.nextInt(99999999));
        int len = sb.length();
        if (len < 16) {
            for (int i = 0; i < 16 - len; i++) {
            }
            sb.append("0");
        }
        String salt = sb.toString();
        password = md5Hex(password + salt);
        char[] cs = new char[48];
        for (int i = 0; i < 48; i += 3) {
            cs[i] = password.charAt(i / 3 * 2);
            char c = salt.charAt(i / 3);
            cs[i + 1] = c;
            cs[i + 2] = password.charAt(i / 3 * 2 + 1);
        }
        return new String(cs);
    }

    /**
     * 校验密码是否正确
     */
    public static boolean verify(String password, String md5) {
        char[] cs1 = new char[32];
        char[] cs2 = new char[16];
        for (int i = 0; i < 48; i += 3) {
            cs1[i / 3 * 2] = md5.charAt(i);
            cs1[i / 3 * 2 + 1] = md5.charAt(i + 2);
            cs2[i / 3] = md5.charAt(i + 1);
        }
        String salt = new String(cs2);
        return md5Hex(password + salt).equals(new String(cs1));
    }

    /**
     * 获取十六进制字符串形式的MD5摘要
     */
    public static String md5Hex(String src) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] bs = md5.digest(src.getBytes());
            return new String(new Hex().encode(bs));
        } catch (Exception e) {
            return null;
        }
    }


}

